<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Premium turns shop' );
?>
					<h2>Ages of Strife: <?php transprint( 'Premium turns shop' ); ?></h2><br />
					<br />
					<?php
include_once ( "old_database_connection.php" );
include ( "utils.php" );
$query = "SELECT open_shop FROM game LIMIT 1";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( $row[ 'open_shop' ] == 1 ) {
    $rulers = Array();
    $buddies = Array();
    $search = "";
	if( isset( $_GET[ 'search' ] ) && formatInput( $_GET[ 'search' ] ) != '' ) {
		$search = formatInput( $_GET[ 'search' ] );
		$query = "SELECT * " .
				"FROM ruler " .
				"WHERE name LIKE '%$search%' " .
				"ORDER BY name " .
				"LIMIT 10";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	    while ( $row = mysql_fetch_object( $result ) ) {
			$rulers[] = $row;
	    }
	}
	else {
		$query = "SELECT ruler.name, ruler.user_id " .
				"FROM buddy " .
				"JOIN users ON ( users.user_id = buddy.user_id1 OR users.user_id = buddy.user_id2 ) " .
				"JOIN ruler ON users.user_id = ruler.user_id " .
				"WHERE ( user_id1 = " . $_SESSION[ 'user_id' ] . " OR user_id2 = " . $_SESSION[ 'user_id' ] . " ) AND " .
						"turn1 = 1 AND turn2 = 1 AND ruler1 = 1 AND ruler2 = 1 AND users.user_id != " . $_SESSION[ 'user_id' ] . " " .
				"ORDER BY name DESC ";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	    while ( $row = mysql_fetch_object( $result ) ) {
			$buddies[] = $row;
	    }
	}
					?>
					<div class="blok collapsable closed" id="gift-information-blok">
						<a title="<?php transprint( 'Click to expand or collapse' ); ?>" href="javascript:void(null)" onclick="toggleBlok( 'gift-information-blok' );"><strong><?php transprint( 'How to give a gift' ); ?></strong></a><br />
						<ul>
							<li><?php transprint( 'You can select one of your buddies from the drop down menu to give premium turns to' ); ?></li>
							<li><?php transprint( 'If the ruler you want to give turns to is not in your buddy list you can perform a search, enter the entire or part of the ruler name and press search' ); ?></li>
							<li><?php transprint( 'If you want to send the premium turns anonymously check the box anonymous gift box' ); ?></li>
							<li><?php transprint( 'When you click confirm you will be taken to the shopping page, where you can select how many premium turns to give' ); ?></li>
						</ul>
						<?php transprint( 'To go back to the shop without selecting a ruler select none and click confirm or click on the link at the bottom of the page.' ); ?>
					</div>
					<br />					
					<div class="blok collapsable open" id="item-list-blok">
						<a title="<?php transprint( 'Click to expand or collapse' ); ?>" href="javascript:void(null)" onclick="toggleBlok( 'item-list-blok' );"><strong><?php transprint( 'Select beneficiary' ); ?></strong></a><br />
						<br />
						<form method="get" action="gift.php">
							<?php transprint( 'Search for a ruler' ); ?>: <input type="text" size="20" name="search" value="<?php print( $search ); ?>" /> <input type="submit" value="<?php transprint( 'Search' ); ?>" /><br />
						</form>
						<?php transprint( 'Enter a ruler name or part of a ruler name and press search (maximum of 10 results).' ); ?><br />
						<br />
						<form action="shop.php" method="get">
<?php
	if( count( $rulers ) > 0 ) {
?>
							<strong><?php transprint( 'Found' ); ?> <?php 
		if( count( $rulers ) == 1 ) {
			print( '1 ' . translate( 'ruler' ) );
		} 
		else {
			print( count( $rulers ) . ' ' . translate( 'rulers' ) );
		}
		?></strong> (<a href="gift.php"><?php transprint( 'Show buddies' ); ?></a>)<br />
<?php
	}
	else {
?>
							<strong><?php transprint( 'Buddies' ); ?> (<?php print( count( $buddies ) ); ?> <?php transprint( 'applicable' ); ?>)</strong><br />
<?php
	}
?>
							<?php transprint( 'Select ruler' ); ?>: <select name="user_id">
							    <option value="-1"><?php transprint( 'None' ); ?></option>
<?php
	if( count( $rulers ) > 0 ) {
		foreach( $rulers AS $ruler ) {
			print( "<option value=\"$ruler->user_id\">$ruler->name</option>\r\n" );
		}
	}
	else {
		foreach( $buddies AS $buddy ) {
			print( "<option value=\"$buddy->user_id\">$buddy->name</option>\r\n" );
		}
	}
?>
							</select><br />
							<br />
							<input type="checkbox" name="anonymous" value="1" /> <?php transprint( 'Anonymous gift' ); ?><br />
							<input type="submit" value="<?php transprint( 'Confirm' ); ?>" /> (<?php transprint( 'confirm beneficiary and go to item selection' ); ?>)<br />
						</form>
						<br />
						<?php transprint( 'To go back to the shop without selecting a beneficiary click' ); ?> <a href="shop.php"><?php transprint( 'here' ); ?></a>.
					</div>
					<?php
}
else {
?>
					<div class="blok">
						<?php transprint( 'The shop is disabled on this server.' ); ?><br />
					</div>
<?php
}
?>
					<script type="text/javascript" src="shop.js"></script>
<?php
aos_footer();
?>